import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import PrivateChat from '../views/PrivateChat.vue'
import Login from '../views/Login.vue'
import Register from '../views/Register.vue'
import chatWindow from '@/components/ChatWindow.vue'
import UserList from '@/components/UserList.vue'
import Post from '@/components/Post.vue'
import Posts from '@/components/Posts.vue'
import EditPost from '@/components/EditPost.vue'
import GroupChat from '../components/GroupChat.vue'
// import store from '../store/index'

Vue.use(VueRouter)

const routes = [{
        path: '/',
        name: 'home',
        component: Home,
        meta: {
            requireAuth: true
        }

    },
    {
        path: '/login',
        component: Login,

    },
    {
        path: '/register',
        component: Register
    },
    {
        path: '/private_chat/:id',
        component: PrivateChat,
        meta: {
            requireAuth: true
        }
    },
    {
        path: '/group_chat/:id',
        component: GroupChat,
        meta: {
            requireAuth: true
        }
    },
    {
        path: '/chat_window/:id',
        component: chatWindow,
        meta: {
            requireAuth: true
        }
    },
    {
        path: '/all_users',
        component: UserList,
        meta: {
            requireAuth: true
        }
    },
    {
        path: '/post',
        component: Post,
        meta: {
            requireAuth: true
        }
    },
    {
        path: '/posts',
        component: Posts,
        meta: {
            requireAuth: true
        }
    },
    {
        path: '/edit_post',
        component: EditPost,
        meta: {
            requireAuth: true
        }
    },

    {
        path: '/about',
        name: 'about',
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () =>
            import ( /* webpackChunkName: "about" */ '../views/About.vue')
    },
    {
        path: '/not_found',
        component: {
            template: `<h1>Not Found<h1>`
        }
    },
    {
        path: '*',
        redirect: '/not_found'
    }

]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})

router.beforeEach((to, from, next) => {

    let isLogin = localStorage.getItem('isLogin');

    if (isLogin == 1) {
        window.console.log(1);
        next();
    } else {
        if (to.meta.requireAuth) {
            window.console.log(2);
            next('/login');
        } else {
            window.console.log(3);
            next();
        }
    }

})

export default router